home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / gnu / gcctest / tests03.zoo / tprintf.c < prev    next >
C/C++ Source or Header  |  1992-04-27  |  5KB  |  167 lines

  1. /* Copyright (C) 1991, 1992 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3.  
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public License as
  6. published by the Free Software Foundation; either version 2 of the
  7. License, or (at your option) any later version.
  8.  
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. Library General Public License for more details.
  13.  
  14. You should have received a copy of the GNU Library General Public
  15. License along with the GNU C Library; see the file COPYING.LIB.  If
  16. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  17. Cambridge, MA 02139, USA.  */
  18.  
  19. #include "ansidecl.h"
  20. #ifdef    BSD
  21. #include </usr/include/stdio.h>
  22. #include <strings.h>
  23. #define EXIT_SUCCESS 0
  24. #else
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #endif
  29.  
  30.  
  31. void
  32. DEFUN(fmtchk, (fmt), CONST char *fmt)
  33. {
  34.   (void) fputs(fmt, stdout);
  35.   (void) printf(":\t`");
  36.   (void) printf(fmt, 0x12);
  37.   (void) printf("'\n");
  38. }
  39.  
  40. void
  41. DEFUN(fmtst1chk, (fmt), CONST char *fmt)
  42. {
  43.   (void) fputs(fmt, stdout);
  44.   (void) printf(":\t`");
  45.   (void) printf(fmt, 4, 0x12);
  46.   (void) printf("'\n");
  47. }
  48.  
  49. void
  50. DEFUN(fmtst2chk, (fmt), CONST char *fmt)
  51. {
  52.   (void) fputs(fmt, stdout);
  53.   (void) printf(":\t`");
  54.   (void) printf(fmt, 4, 4, 0x12);
  55.   (void) printf("'\n");
  56. }
  57.  
  58. typedef struct {
  59.   char          *str;
  60.   unsigned long data;
  61. } DATA;
  62.  
  63. static DATA dat[] = {
  64.   {"1",         1},
  65.   {"2",         2},
  66.   {"65535",     65535},
  67.   {"65536",     65536},
  68.   {"70000",     70000},
  69.   {"1289823",     1289823},
  70.   {"2147483647", 0x7fffffffL},
  71.   {"4294967295", 0xffffffffL},
  72.   {"0",         0},
  73.   {0,         0}
  74. };
  75.  
  76. void 
  77. DEFUN_VOID(test_print_unsigned_longs)
  78. {
  79.     DATA *d = dat;
  80.    
  81.     for(; d->str; d++) {
  82.     printf("Length of \"%s\" is %lu Value is %lu\n",
  83.      d->str, strlen(d->str), d->data);
  84.     }
  85. }
  86.  
  87. int
  88. DEFUN_VOID(main)
  89. {
  90.   static char shortstr[] = "Hi, Z.";
  91.   static char longstr[] = "Good morning, Doctor Chandra.  This is Hal.  \
  92. I am ready for my first lesson today.";
  93.  
  94.   fmtchk("%.4x");
  95.   fmtchk("%04x");
  96.   fmtchk("%4.4x");
  97.   fmtchk("%04.4x");
  98.   fmtchk("%4.3x");
  99.   fmtchk("%04.3x");
  100.  
  101.   fmtst1chk("%.*x");
  102.   fmtst1chk("%0*x");
  103.   fmtst2chk("%*.*x");
  104.   fmtst2chk("%0*.*x");
  105.  
  106. #ifndef    BSD
  107.   printf("bad format:\t\"%z\"\n");
  108.   printf("nil pointer:\t\"%p\"\n", (PTR) NULL);
  109. #endif
  110.  
  111.   printf("decimal negative:\t\"%d\"\n", -2345);
  112.   printf("octal negative:\t\"%o\"\n", -2345);
  113.   printf("hex negative:\t\"%x\"\n", -2345);
  114.   printf("long decimal number:\t\"%ld\"\n", -123456);
  115.   printf("long octal negative:\t\"%lo\"\n", -2345L);
  116.   printf("long unsigned decimal number:\t\"%lu\"\n", -123456);
  117.   printf("zero-padded LDN:\t\"%010ld\"\n", -123456);
  118.   printf("left-adjusted ZLDN:\t\"%-010ld\"\n", -123456);
  119.   printf("space-padded LDN:\t\"%10ld\"\n", -123456);
  120.   printf("left-adjusted SLDN:\t\"%-10ld\"\n", -123456);
  121.  
  122.   printf("zero-padded string:\t\"%010s\"\n", shortstr);
  123.   printf("left-adjusted Z string:\t\"%-010s\"\n", shortstr);
  124.   printf("space-padded string:\t\"%10s\"\n", shortstr);
  125.   printf("left-adjusted S string:\t\"%-10s\"\n", shortstr);
  126.   printf("null string:\t\"%s\"\n", (char *)NULL);
  127.   printf("limited string:\t\"%.22s\"\n", longstr);
  128.  
  129.   printf("e-style >= 1:\t\"%e\"\n", 12.34);
  130.   printf("e-style >= .1:\t\"%e\"\n", 0.1234);
  131.   printf("e-style < .1:\t\"%e\"\n", 0.001234);
  132.   printf("e-style big:\t\"%.60e\"\n", 1e20);
  133.   printf ("e-style == .1:\t\"%e\"\n", 0.1);
  134.   printf("f-style >= 1:\t\"%f\"\n", 12.34);
  135.   printf("f-style >= .1:\t\"%f\"\n", 0.1234);
  136.   printf("f-style < .1:\t\"%f\"\n", 0.001234);
  137.   printf("g-style >= 1:\t\"%g\"\n", 12.34);
  138.   printf("g-style >= .1:\t\"%g\"\n", 0.1234);
  139.   printf("g-style < .1:\t\"%g\"\n", 0.001234);
  140.   printf("g-style big:\t\"%.60g\"\n", 1e20);
  141.  
  142. #define FORMAT "|%12.4f|%12.4e|%12.4g|\n"
  143.   printf (FORMAT, 0.0, 0.0, 0.0);
  144.   printf (FORMAT, 1.0, 1.0, 1.0);
  145.   printf (FORMAT, -1.0, -1.0, -1.0);
  146.   printf (FORMAT, 100.0, 100.0, 100.0);
  147.   printf (FORMAT, 1000.0, 1000.0, 1000.0);
  148.   printf (FORMAT, 10000.0, 10000.0, 10000.0);
  149.   printf (FORMAT, 12345.0, 12345.0, 12345.0);
  150.   printf (FORMAT, 100000.0, 100000.0, 100000.0);
  151.   printf (FORMAT, 123456.0, 123456.0, 123456.0);
  152. #undef    FORMAT
  153.  
  154. #if __USE_GNU /* GLIBC extension */
  155.   {
  156.     char buf[20];
  157.     printf ("snprintf (\"%%30s\", \"foo\") == %d, \"%.*s\"\n",
  158.         snprintf (buf, sizeof (buf), "%30s", "foo"), sizeof (buf), buf);
  159.   }
  160. #endif
  161.  
  162.   /* more tests ++jrb */
  163.   test_print_unsigned_longs();
  164.  
  165.   return(EXIT_SUCCESS);
  166. }
  167.